package com.changgou.order.controller;

import com.changgou.entity.Result;
import com.changgou.order.feign.CartFeign;
import com.changgou.order.pojo.Order;
import com.changgou.pay.feign.WxPayFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Map;

@Controller
@RequestMapping("/wxpay")
public class PayController {
	@Autowired
	private CartFeign cartFeign;
	@Autowired
	private WxPayFeign wxPayFeign;
	@GetMapping
	public String wxPay(String orderId, Model model){
		//根据orderId查询订单
		Result<Order> orderResult = cartFeign.findById(orderId);
		if(orderResult.getData()==null){
			return "fail";
		}
		Order order = orderResult.getData();
		//判断支付状态
		if(!"0".equals(order.getPayStatus())){
			return "fail";
		}
		Result result = wxPayFeign.nativePay(orderId, order.getPayMoney());
		if(result.getData()==null){
			return "fail";
		}
		Map payMap = (Map) result.getData();
		payMap.put( "payMoney", order.getPayMoney() );
		payMap.put( "orderId" ,orderId );
		model.addAllAttributes( payMap );
		return "wxpay";
	}
}
